using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxTabControl;
using DevExpress.Web.ASPxUploadControl;
using DevExpress.Web.ASPxHtmlEditor;
using DevExpress.Web.ASPxCallback;
using Indeco.G24.Web;
using Indeco.G24.Data;
using Indeco.G24.Services;
using Indeco.G24.Entities;
namespace Indeco.G24.Website.News.Admin
{
    public partial class TinTucSuKien : G24Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {                
            Response.Cache.SetNoStore();
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            loadData();
            TinTucSuKienService service = new TinTucSuKienService();
            Indeco.G24.Entities.TinTucSuKien ttTieuDiem = new Indeco.G24.Entities.TinTucSuKien();
            string action = Request.Params["action"];
            if (!string.IsNullOrEmpty(action))
            {
                string idTinTuc = Request.Params["idTintuc"];
                string idTinTucDanhMuc = Request.Params["idTinTucDanhMuc"];
                string id = Request.Params["id"];
                if (action.Equals("Insert"))
                {
                    try
                    {
                        Guid idtt = new Guid(idTinTuc);
                        Guid idttdm = new Guid(idTinTucDanhMuc);
                        ttTieuDiem.IDTinTuc = idtt;
                        ttTieuDiem.IDTinTucDanhMuc = idttdm;
                        bool chk = service.DeepSave(ttTieuDiem);
                        if (chk)
                        {                           
                            Response.Write("{\"sucess\":\"true\",\"id\":\"" + id + "\"}");
                            Response.Flush();
                            Response.End();

                        }
                        else
                        {
                            Response.Write("{\"sucess\":\"false\"}");
                            Response.Flush();
                            Response.End();
                        }
                    }
                    catch
                    {

                    }
                }
                else if (action.Equals("Remove"))
                {
                    try
                    {
                        Guid idtt = new Guid(idTinTuc);
                        Guid idttdm = new Guid(idTinTucDanhMuc);
                        ttTieuDiem.IDTinTuc = idtt;
                        ttTieuDiem.IDTinTucDanhMuc = idttdm;
                        bool chk = service.Delete(ttTieuDiem);
                        if (chk)
                        {                           
                            Response.Write("{\"sucess\":\"true\",\"id\":\"" + id + "\"}");
                            Response.Flush();
                            Response.End();
                        }
                        else
                        {
                            Response.Write("{\"sucess\":\"false\"}");
                            Response.Flush();
                            Response.End();
                        }
                    }
                    catch
                    {

                    }
                }
            }
        }
        private void loadData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Ten", typeof(string));
            dt.Columns.Add("TieuDe", typeof(string));
            dt.Columns.Add("IDTinTucDanhMuc", typeof(string));
            dt.Columns.Add("IDTinTuc", typeof(string));
            dt.Columns.Add("isCheck", typeof(bool));
            TinTucService tinService = new TinTucService();
            TList<Indeco.G24.Entities.TinTuc> tin = tinService.GetAll();
            if (tin != null && tin.Count > 0)
            {
                int i = 0;
                foreach (Indeco.G24.Entities.TinTuc t in tin)
                {
                    dt.Rows.Add(new object[] { i, getTen(t.IDTinTucDanhMuc.ToString()), t.TieuDe.ToString(), t.IDTinTucDanhMuc.ToString(), t.IDTinTuc.ToString(), isCheck(t.IDTinTuc.ToString(), t.IDTinTucDanhMuc.ToString()) });
                    i++;
                }
            }
            ASPxGridView3.DataSource = dt;
            ASPxGridView3.KeyFieldName = "ID";
            ASPxGridView3.DataBind();
        }
        private string getTen(string id)
        {
            TinTucDanhMucService seviced = new TinTucDanhMucService();
            Indeco.G24.Entities.TinTucDanhMuc tin = seviced.Find("id_tintucdanhmuc='" + id + "'")[0];
            if (tin != null && !string.IsNullOrEmpty(tin.Ten))
                return tin.Ten;
            else
                return null;
        }
        private bool isCheck(string idTin, string idDanhMuc)
        {
            int outcount = 0;
            TinTucSuKienService sevicet = new TinTucSuKienService();
            TList<Indeco.G24.Entities.TinTucSuKien> tin = sevicet.GetPaged("id_tintuc='" + idTin + "' and id_tintucdanhmuc='" + idDanhMuc + "'", "", 0, 10, out outcount);
            if (tin != null && tin.Count > 0)
            {
                return true;
            }
            else
                return false;
        }
    }
}
